package com.fr.report.io;

import com.fr.base.FRContext;
import com.fr.base.Inter;
import com.fr.base.core.FT;
import com.fr.base.file.CacheManager;
import com.fr.report.PageSet;
import com.fr.report.PaperSetting;
import com.fr.report.RWorkBook;
import com.fr.report.Report;
import com.fr.report.ReportSettings;
import com.fr.report.ResultWorkBook;
import com.fr.report.core.cal.LayerPageReport.LayerPageSet;
import com.fr.report.io.core.LargeDataExcelExporterReport;
import com.fr.third.org.apache.poi.hssf.usermodel.HSSFWorkbook;
import com.fr.util.Utils;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.zip.ZipOutputStream;

public class LargeDataPageExcelExporter extends ExcelExporter
{
  private boolean isPage = false;

  public LargeDataPageExcelExporter(List paramList, boolean paramBoolean)
  {
    super(paramList);
    this.isPage = paramBoolean;
  }

  public void export(OutputStream paramOutputStream, ResultWorkBook paramResultWorkBook)
    throws Exception
  {
    export(paramOutputStream, paramResultWorkBook, false);
  }

  public void export(OutputStream paramOutputStream, ResultWorkBook paramResultWorkBook, boolean paramBoolean)
    throws Exception
  {
    File localFile1 = new File(FRContext.getCacheManager().getCacheDirectory(), "excel-temp");
    File localFile2 = new File(localFile1, "TEMP_" + System.currentTimeMillis() + "_" + (int)(Math.random() * 1000.0D));
    for (int i = 0; localFile2.exists(); ++i)
      localFile2 = new File(localFile1, "TEMP_" + System.currentTimeMillis() + "_" + (int)(Math.random() * 1000.0D) + i);
    Utils.mkdirs(localFile2);
    localFile2.deleteOnExit();
    ZipOutputStream localZipOutputStream = new ZipOutputStream(paramOutputStream);
    try
    {
      for (int j = 0; j < paramResultWorkBook.getReportCount(); ++j)
      {
        Report localReport = paramResultWorkBook.getReport(j);
        if (localReport == null)
          break label265:
        if (localReport.getReportSettings() == null)
          localReport.setReportSettings(new ReportSettings());
        localReport.getReportSettings().setPageOrder(1);
        export2temp(localFile2, paramResultWorkBook.getReportName(j), localReport, (this.paperSettingList == null) ? null : (PaperSetting)this.paperSettingList.get(j), paramBoolean);
      }
      label265: FRContext.getLogger().log(Level.INFO, "start compress files");
      Utils.zip(localZipOutputStream, localFile2, "");
      localZipOutputStream.closeEntry();
      localZipOutputStream.close();
      FRContext.getLogger().log(Level.INFO, "complete compress files");
    }
    finally
    {
      Utils.deleteFile(localFile2);
    }
  }

  private void export2temp(File paramFile, String paramString, Report paramReport, PaperSetting paramPaperSetting, boolean paramBoolean)
    throws Exception
  {
    if ((paramString == null) || (paramReport == null))
      return;
    PageSet localPageSet = paramReport.generateReportPageSet(paramPaperSetting);
    if (localPageSet instanceof LayerPageReport.LayerPageSet)
      localPageSet = ((LayerPageReport.LayerPageSet)localPageSet).getTraversingCase(this.isPage);
    else
      localPageSet = localPageSet.getTraversingCase();
    try
    {
      int i = 1;
      FRContext.getLogger().log(Level.INFO, "\"" + paramString + "\"" + Inter.getLocText("Prepare_Export"));
      FT localFT = new FT(0, 0);
      while (localFT.to != -1)
      {
        localFT.from = localFT.to;
        FileOutputStream localFileOutputStream = new FileOutputStream(paramFile.getAbsolutePath() + File.separator + paramString + "_" + i + ".xls");
        RWorkBook localRWorkBook = new RWorkBook();
        try
        {
          localRWorkBook.addReport(paramString, new LargeDataExcelExporterReport(paramReport, localPageSet, localFT, this.isPage));
          super.export(localFileOutputStream, localRWorkBook, false);
          FRContext.getLogger().log(Level.INFO, "\"" + paramString + "_" + i + ".xls\"" + Inter.getLocText("Finish_Export"));
        }
        finally
        {
          localFileOutputStream.close();
        }
        ++i;
      }
    }
    finally
    {
      if ((!(paramBoolean)) || (!(localPageSet.pageCached())))
        localPageSet.release();
    }
    FRContext.getLogger().log(Level.INFO, "\"" + paramString + Inter.getLocText("Finish_Export"));
  }

  protected void exportBook(ResultWorkBook paramResultWorkBook, HSSFWorkbook paramHSSFWorkbook, List paramList1, List paramList2, List paramList3, boolean paramBoolean)
    throws Exception
  {
    Report localReport;
    for (int i = 0; i < paramResultWorkBook.getReportCount(); ++i)
    {
      localReport = paramResultWorkBook.getReport(i);
      paramList3.add(localReport);
    }
    for (i = 0; i < paramResultWorkBook.getReportCount(); ++i)
    {
      localReport = (Report)paramList3.get(i);
      innerExportReport(localReport, paramResultWorkBook.getReportExportAttr(), paramResultWorkBook.getReportName(i), paramHSSFWorkbook, paramList1, paramList2, i);
    }
  }
}